<template>
  <!-- 导航栏组件 -->
  <nav class="modern-navbar">
    <div class="navbar-left">
      <img src="@/assets/logo.png" class="logo" alt="Logo" />
      <span class="brand">爱迪智考<span style="font-size: 12px; color: #999;">Beta</span></span>
    </div>
    <div class="navbar-center">
                  <router-link        v-for="item in navItems"        :key="item.path"        :to="item.path"        class="nav-link"        active-class="active-link"      >
        <span class="icon" v-html="item.iconSvg"></span>
        <span>{{ item.label }}</span>
      </router-link>
    </div>
    <div class="navbar-right">
      <div class="avatar-container" ref="avatarContainer">
        <div class="avatar" @click="toggleDropdown">
          <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="7" r="4"/><path d="M5.5 21a8.38 8.38 0 0 1 13 0"/></svg>
        </div>
        <!-- 下拉菜单 -->
        <div class="dropdown-menu" v-show="showDropdown">
          <div class="dropdown-item" @click="logout">
            <span class="icon"><i class="fas fa-sign-out-alt"></i></span>
            <span>退出登录</span>
          </div>
        </div>
      </div>
    </div>
  </nav>
</template>

<script>
export default {
  // 组件名称
  name: 'Navbar',
  data() {
    return {
      navItems: [
              {          label: '题库管理',          path: '/question-bank',          iconSvg: `<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2"/><path d="M3 9h18"/><path d="M9 21V9"/></svg>`        },
        {
          label: '试卷管理',
          path: '/paper-index/paper-list',
          iconSvg: `<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="4" width="16" height="16" rx="2"/><path d="M8 2v4"/><path d="M16 2v4"/><path d="M4 10h16"/></svg>`
        },
        {
          label: '考试管理',
          path: '/exam-manage',
          iconSvg: `<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="2" y="4" width="20" height="16" rx="2"/><path d="M8 2v4"/><path d="M16 2v4"/><path d="M2 10h20"/></svg>`
        }
      ],
      showDropdown: false
    }
  },
  mounted() {
    // 添加全局点击事件监听，点击其他地方关闭下拉菜单
    document.addEventListener('click', this.handleClickOutside)
  },
  beforeDestroy() {
    // 组件销毁前移除事件监听
    document.removeEventListener('click', this.handleClickOutside)
  },
  methods: {
    toggleDropdown() {
      this.showDropdown = !this.showDropdown
    },
    handleClickOutside(event) {
      // 如果点击的不是头像容器内的元素，则关闭下拉菜单
      if (this.$refs.avatarContainer && !this.$refs.avatarContainer.contains(event.target)) {
        this.showDropdown = false
      }
    },
    logout() {
      // 清除token
      localStorage.removeItem('token')
      
      // 显示退出成功提示
      // this.$message && this.$message.success('退出登录成功')
      
      // 重定向到登录页
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="less" scoped>
// 定义颜色和阴影等变量
@primary: #4361EE;
@primary-light: #A5B4FC;
@shadow-md: 0 4px 12px rgba(67,97,238,0.08);
// 添加灰度色系
@gray-50: #F9FAFB;
@gray-100: #F3F4F6;
@gray-200: #E5E7EB;
@gray-300: #D1D5DB;
@gray-400: #9CA3AF;
@gray-500: #6B7280;
@gray-600: #4B5563;
@gray-700: #374151;
@gray-800: #1F2937;
@gray-900: #111827;

// 导航栏样式
.modern-navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: linear-gradient(90deg, #e0e7ff 0%, #f8fafc 100%);
  box-shadow: @shadow-md;
  padding: 0 2.5rem;
  height: 60px;
  border-radius: 0 0 18px 18px;
  position: relative;
  z-index: 100;
  transition: box-shadow 0.2s;

  .navbar-left {
    display: flex;
    align-items: center;
    gap: 12px;
    .logo {
      height: 38px;
      border-radius: 8px;
      box-shadow: 0 2px 8px rgba(67,97,238,0.08);
      background: white;
      padding: 2px;
      transition: transform 0.2s;
    }
    .brand {
      font-size: 1.25rem;
      font-weight: bold;
      color: @primary;
      letter-spacing: 2px;
      user-select: none;
    }
  }
  .navbar-center {
    display: flex;
    gap: 2.5rem;
    .nav-link {
      display: flex;
      align-items: center;
      gap: 0.5rem;
      color: #64748B;
      font-size: 1.08rem;
      font-weight: 500;
      text-decoration: none;
      padding: 0.5rem 1.2rem;
      border-radius: 8px;
      transition: background 0.2s, color 0.2s, box-shadow 0.2s;
      .icon {
        display: flex;
        align-items: center;
        svg {
          vertical-align: middle;
        }
      }
      &:hover {
        background: @primary-light;
        color: @primary;
        box-shadow: 0 2px 8px fade(@primary, 10%);
      }
      &.active-link {
        background: @primary;
        color: #fff;
        box-shadow: 0 4px 16px fade(@primary, 15%);
      }
    }
  }
  .navbar-right {
    display: flex;
    align-items: center;
    gap: 16px;
    
    .avatar-container {
      position: relative;
      
      .avatar {
        width: 38px;
        height: 38px;
        border-radius: 50%;
        background: @primary-light;
        display: flex;
        align-items: center;
        justify-content: center;
        color: @primary;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.2s;
        box-shadow: 0 2px 8px fade(@primary, 10%);
        svg {
          width: 22px;
          height: 22px;
        }
        &:hover {
          background: @primary;
          color: white;
          box-shadow: 0 0 0 3px @primary-light;
        }
      }
      
      // 下拉菜单样式
      .dropdown-menu {
        position: absolute;
        top: calc(100% + 8px);
        right: 0;
        background: white;
        border-radius: 8px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        min-width: 140px;
        overflow: hidden;
        z-index: 1000;
        animation: dropdown-fade 0.2s ease;
        
        .dropdown-item {
          padding: 12px 16px;
          display: flex;
          align-items: center;
          gap: 8px;
          color: @gray-700;
          cursor: pointer;
          transition: all 0.2s;
          
          .icon {
            width: 16px;
            height: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: @primary;
          }
          
          &:hover {
            background: @gray-100;
            color: @primary;
          }
        }
      }
    }
  }
  @media (max-width: 900px) {
    padding: 0 1rem;
    .navbar-center { gap: 1.2rem; }
    .brand { font-size: 1.1rem; }
  }
  @media (max-width: 600px) {
    flex-direction: column;
    height: auto;
    padding: 0.5rem 0.5rem 0.2rem 0.5rem;
    .navbar-center { gap: 0.5rem; }
    .brand { display: none; }
    .logo { height: 32px; }
  }
}

// 下拉菜单动画
@keyframes dropdown-fade {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>    